package com.example.fitnessservice.service.impl;

import com.example.fitnessservice.api.exception.CoachException;
import com.example.fitnessservice.api.exception.CourseException;
import com.example.fitnessservice.module.dao.coachmapper.CoachBalanceMapper;
import com.example.fitnessservice.module.dao.courseMapper.*;
import com.example.fitnessservice.module.pojo.CourseApply;
import com.example.fitnessservice.module.pojo.CourseDetails;
import com.example.fitnessservice.module.pojo.CourseDetailsCamp;
import com.example.fitnessservice.service.CoachApproveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * @author feng
 * @version 1.0
 * @&#064;description: TODO
 * @&#064;date 2022/11/27 22:04
 */
@Service
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public class CoachApproveServiceImpl implements CoachApproveService {
    private final double COACH_INCOME = 0.9;
    @Autowired
    CourseMapper courseMapper;
    @Autowired
    CampCourseMapper campCourseMapper;
    @Autowired
    UserCourseMapper userCourseMapper;
    @Autowired
    CourseOrderMapper courseOrderMapper;
    @Autowired
    CourseApplyMapper courseApplyMapper;
    @Autowired
    CoachBalanceMapper coachBalanceMapper;

    @Override
    public List<CourseApply> getAllApppy(Integer coachId) {
        List<CourseApply> courseApplyList = courseApplyMapper.getAllApppy(coachId);
        if (CollectionUtils.isEmpty(courseApplyList)) {
            return null;
        }
        return courseApplyList;
    }

    @Override
    public boolean approve(Integer coachId, Integer userId, Integer courseId, Integer passIf) {
        System.out.println(coachId);
        System.out.println(userId);
        System.out.println(courseId);
        System.out.println(passIf);
        CourseApply courseApply = new CourseApply();
        courseApply.setApplyCoachId(coachId);
        courseApply.setApplyUserId(userId);
        courseApply.setApplyIfAlready(passIf);
        courseApply.setApplyCourseId(courseId);
        if (courseApplyMapper.approve(courseApply) <= 0) {
            throw new CoachException("审批异常");
        }
        System.out.println(courseApplyMapper.approve(courseApply));
        return true;
    }

    @Override
    public boolean takeCourse(Integer courseId, Integer courseCampIf) {

        if (courseCampIf == 1) {
            if (campCourseMapper.findNowCourse(courseId) == null) {
                throw new CoachException("未到开课时间，不允许开课");
            }
            if (campCourseMapper.takeCourse(courseId) <= 0) {
                throw new CoachException("上课操作失败");
            }
        } else {
            if (courseMapper.findNowCourse(courseId) == null) {
                throw new CoachException("未到开课时间，不允许开课");
            }
            if (courseMapper.takeCourse(courseId) <= 0) {
                throw new CoachException("上课操作失败");
            }
        }
        if (courseOrderMapper.takeCourse(courseId, courseCampIf) <= 0) {
            throw new CoachException("订单状态修改失败");
        }
        if (userCourseMapper.takeCourse(courseId, courseCampIf) <= 0) {
            throw new CoachException("个人状态修改失败");

        }
        return true;
    }

    @Override
    public boolean finishCourse(Integer courseId, Integer courseCampIf) {
        Integer studentNum=userCourseMapper.getNormalNum(courseId,courseCampIf);

        Date date = new Date();
        if (courseCampIf == 1) {
            if (campCourseMapper.findNowCourse(courseId) != null) {
                throw new CoachException("未到结束时间，不允许提前结束");
            }
            boolean ifEnd = true;
            List<CourseDetailsCamp> listCamp = campCourseMapper.findCourseDetailsById(courseId);
            if (CollectionUtils.isEmpty(listCamp)) {
                throw new CourseException("无该课程");
            }
            for (CourseDetailsCamp camp : listCamp) {
                if (date.compareTo(camp.getCourseEndTime()) < 0) {
                    ifEnd = false;
                    break;
                }
            }
            if (!ifEnd) {
                if (courseOrderMapper.finishCourse(courseId, courseCampIf) <= 0) {
                    throw new CoachException("订单状态修改失败");
                }
                if (userCourseMapper.finishiCourse(courseId, courseCampIf) <= 0) {
                    throw new CoachException("个人状态修改失败");
                }
                if (coachBalanceMapper.coachGetMoney(studentNum*listCamp.get(0).getCourseMoney() * COACH_INCOME, listCamp.get(0).getCourseId()) <= 0) {
                    throw new CoachException("教练余额增加失败");
                }
            }

        } else {
            if (courseMapper.findNowCourse(courseId) != null) {
                throw new CoachException("未到结束时间，不允许提前结束");
            }
            CourseDetails courseDetailsById = courseMapper.findCourseDetailsById(courseId);
            if (courseDetailsById == null) {
                throw new CourseException("无该课程");
            }
            if (courseOrderMapper.finishCourse(courseId, courseCampIf) <= 0) {
                throw new CoachException("订单状态修改失败");
            }
            if (userCourseMapper.finishiCourse(courseId, courseCampIf) <= 0) {
                throw new CoachException("个人状态修改失败");
            }
            if (coachBalanceMapper.coachGetMoney(studentNum*courseDetailsById.getCourseMoney() * COACH_INCOME, courseDetailsById.getCourseCoachId()) <= 0) {
                throw new CoachException("教练余额增加失败");
            }
        }
        return true;
    }


}
